class Student(object):
    def __init__(self,name):
        self.name = name

    def __str__(self): #可以返回一个好看一点的字符串，不然返回的是一串内存地址
        return 'Student object (name:%s)'%self.name

    __repr__ = __str__
print(Student('Micheal'))
s = Student('Jack')
print(s)

class Fib(object):
    def __init__(self):
        self.a,self.b=0,1

    def __iter__(self): #该方法返回一个迭代对象，然后Python的for循环就会不断调用该迭代对象的__next__()方法拿到循环的下一个值，直到遇到StopIteration错误
        return self

    def __next__(self):
        self.a,self.b=self.b,self.a+self.b
        if self.a>1000:
            raise StopIteration()
        return self.a

    def __getitem__(self,n):    #让斐波那契数列表现的像list一样，需要添加一个__getitem__方法
        if isinstance(n,int):   #n是索引
            a,b =1,1
            for x in range(n):  
                a,b=b,a+b
            return a
        if isinstance(n,slice): #n是切片
            start = n.start
            stop = n.stop
            if start is None:
                start = 0
            a,b = 1,1
            L = []
            for x in range(stop):
                if x>=start:
                    L.append(a)
                a,b = b,a+b
            return L


#for n in Fib():
    #print(n)

f = Fib()
print(f[0])
print(f[0:5])